package com.yunfei.common.auto.cratePage;




import com.yunfei.common.auto.GeneratorBase;
import com.yunfei.common.auto.util.CapitalizeUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
/**
 * 自动创建文件上传所需要的js文件
 * @author  贤云
 *
 */
public class GeneratorUploadJs extends GeneratorBase {
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(GeneratorBase.DRIVER);
            String url = GeneratorBase.URL;
            String user = GeneratorBase.NAME;
            String pass = GeneratorBase.PASS;
            conn = DriverManager.getConnection(url,user,pass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void main(String[] args){
        GeneratorUploadJs generatorInitPageDB=new GeneratorUploadJs();
        generatorInitPageDB.generatorInitPage();
    }
    public  void generatorInitPage(){
        boolean whetherCreateUploadJs=false;
        Connection conn=getConnection();
        String sql="select * from "+tablename;
        PreparedStatement stmt;
        String content="";
        try {
            stmt = conn.prepareStatement(sql);
            ResultSet rs=stmt.executeQuery(sql);
            ResultSetMetaData data=rs.getMetaData();
            DatabaseMetaData dbmd=conn.getMetaData();
            while(rs.next()){
                for(int i = 1 ; i<= data.getColumnCount() ; i++){
                    //获得所有列的数目及实际列数
                    int columnCount=data.getColumnCount();
                    //获得指定列的列名
                    String columnName = data.getColumnName(i);
                    String columnLabel= CapitalizeUtil.initcap(data.getColumnLabel(i));
                    String columnRemark="";
                    String columnRemarkAll="";
                    ResultSet result = dbmd.getColumns(null, "%", tablename, "%");
                    boolean isHasColumnRemark=false;
                    while(result.next()){
                        if(!isHasColumnRemark){//只有remark没有的时候才赋值，有的时候就不用多次赋值了
                            if(columnName.contains(result.getString("COLUMN_NAME"))){
                                isHasColumnRemark=true;
                                columnRemark= CapitalizeUtil.initcap(result.getString("REMARKS"));
                                columnRemarkAll=columnRemark;
                                //以分号分隔，可以添加额外的信息
                                columnRemark=columnRemark.split(";")[0];
                            }
                        }

                    }
                    boolean isNullableBoolean=false;
                    //有图片的单独处理
                    if(!content.contains("id=\"" + columnLabel + "\"")&&columnRemarkAll.contains("isImage")){//有图片的内容处理
                        content+="    /**图片手机缩略图上传**/\n" +
                                "    layui.upload({\n" +
                                "        url: '../html5/upload.shtml'\n" +
                                "        ,elem: '#"+columnLabel+"ImageUploadId'\n" +
                                "        ,unwrap: true\n" +
                                "        ,before: function(input){\n" +
                                "            //加载层\n" +
                                "            dialogInde = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2\n" +
                                "        }\n" +
                                "        ,success: function(src,input){\n" +
                                "            layer.close(dialogInde);\n" +
                                "            $(\"#"+columnLabel+"ImgSrc\").attr('src',src);\n" +
                                "            var id=$(input).attr('data-id');\n" +
                                "            $(\"#\"+id).val(src);\n" +
                                "        },complete:function(){\n" +
                                "            layer.close(dialogInde);\n" +
                                "        }\n" +
                                "    });";
                    }

                    }
                }

            /****************************************自动生成upload.js页面 start******************************************************************************/
            try {
                content=getInitPageContent(pageTitle, pageOutPath,content);
                File directory = new File("");
                String outputPath = directory.getAbsolutePath()+ "/web/"+ pageOutPath+"/js/upload.js";
                File file=new File(outputPath);
                file.getParentFile().mkdirs();
                FileWriter fw = new FileWriter(outputPath);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(content);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            /****************************************自动生成init.jsp页面 end******************************************************************************/
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
        }
    }
    public static String parse(String all) {
        String comment = null;
        int index = all.indexOf("COMMENT='");
        if(index < 0) {
            return "";
        }
        comment = all.substring(index+9);
        comment = comment.substring(0,comment.length() - 1);
        return comment;
    }
    /**
     *生成初始化页面
     * @param title 页面标题
     * @param path s9/v3/pastInvestmentCase 页面所在路径
     * @return
     */
    public static  String getInitPageContent(String title,String path,String authoCreateElement){
        String content="";
        content="var dialogInde=0;\n" +
                "layui.use('upload', function(){\n" +
                "    layui.upload({\n" +
                "        url: ''\n" +
                "        ,success: function(src){\n" +
                "        }\n" +
                "    });\n" ;
                content+=authoCreateElement;
                content+="});";
        return content;
    }
}
